package com.dsmbase.corp.common;

import java.util.List;

import com.dsmbase.corp.flow.domain.FlowBranch;
import com.dsmbase.corp.flow.domain.FlowNode;

public class FlowUtil {

	public static FlowNode getStartNode(List<FlowNode> nodes, List<FlowBranch> flowBranch) {
		for (FlowNode node : nodes) {
			int countasStart = 0;
			int countAsEnd = 0;
			for(FlowBranch branch : flowBranch) {
				if (branch.getFlowId() == node.getFlowId()) {
					if (branch.getNodeStart() == node.getId() && branch.getNodeEnd() == node.getId()) {
						return node;
					}
					if (branch.getNodeStart() == node.getId()) {
						countasStart++;
					}
					if (branch.getNodeEnd() == node.getId()) {
						countAsEnd++;
					}
				}
			}
			if (countasStart == 1 && countAsEnd == 0) {
				return node;
			}
		}
	    return null;
	}
	
	public static int getNextNodeId(FlowNode node, List<FlowBranch> flowBranch) {
		for (FlowBranch branch : flowBranch) {
			if (branch.getFlowId() == node.getFlowId() && branch.getNodeStart() == node.getId()) {
				if (node.getId() != branch.getNodeEnd()) {
					return branch.getNodeEnd();
				}
			}
		}
		return -1;
	}
	
}
